package com.pac.weixin.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.weixin.bean.WxUserBean;
import com.pac.weixin.dao.WxUserDao;
import com.pac.weixin.service.WxUserService;

/**
 * 微信用户信息Service实现类
 * 
 * @author lujun.chen
 * @version [版本号, 2015年3月27日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "wxUserService")
public class WxUserServiceImpl implements WxUserService {

	private static Logger logger = Logger.getLogger(WxUserServiceImpl.class);

	@Autowired
	private WxUserDao wxUserDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public String addWxUser(WxUserBean wxUserBean) {

		if (null == wxUserBean) {
			logger.error("添加微信用户信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(wxUserBean.getOpenid())) {
			logger.error("添加微信用户信息出错，微信openid为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "微信openid为空");
		}

		return wxUserDao.addWxUser(wxUserBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delWxUser(WxUserBean wxUserBean) {

		if (null == wxUserBean) {
			logger.error("删除微信用户信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(wxUserBean.getId())) {
			logger.error("删除微信用户信息出错，ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "ID为空");
		}

		return wxUserDao.delWxUser(wxUserBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateWxUser(WxUserBean wxUserBean) {
		if (null == wxUserBean) {
			logger.error("更新微信用户信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(wxUserBean.getId())
				&& StringTools.isNull(wxUserBean.getOpenid())) {
			logger.error("更新微信用户信息出错，ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "ID为空");
		}
		return wxUserDao.updateWxUser(wxUserBean);
	}

	@Override
	public List<WxUserBean> queryWxUser(WxUserBean wxUserBean) {
		return wxUserDao.queryWxUser(wxUserBean);
	}

}
